package game;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.util.LinkedList;

abstract class BaseLevel extends Scene 
{
	Background bg;
	Personagem player;
	GameObject timeBox;
	TimeLevel timeLevel;
	SceneObject objetosGame;
	RandomManager managerAlerts;
	Button Loja;
	private boolean LojaAtiva = false;
	private int valueID, valueObject;
	private float Contador;
	LojaManager loja;
	GameObject sairLoja;
	LinkedList<ObjectsCombat> objs;
	
	static int SlotTecnico = 4;
	static int SlotConsertaPoste = 3;
	static int SlotConsertaAntena = 3;

	GameObject messageTecnico;
	GameObject messageAntena;
	GameObject messagePoste;
	
	Tecnico tecnico;
	ConsertaAntena antena;
	ConsertaPoste poste;
	Trabalhadores trabalhadorSelecionado;

	
	public int numeroTecnico = 0, numeroPoste = 0, numeroAntena = 0;
	
	boolean MouseReleased;

	LinkedList<Integer> sortedInts;
	
	//Camera Variables
		final float speed = 5;
		//GameObject camUp, camLeft, camRight, camDown;
	
	public BaseLevel(int levelDificulty) 
	{
		super();
		objetosGame = new SceneObject();
		sortedInts = new LinkedList<Integer>();
		messageTecnico = new GameObject(new Point(0, 320), new Point(200, 180));
		messageTecnico.setImage(32);
		
		messageAntena = new GameObject(new Point(100, 320), new Point(200, 180));
		messageAntena.setImage(34);
		
		messagePoste = new GameObject(new Point(200, 320), new Point(200, 180));
		messagePoste.setImage(33);
		
		player = new Personagem(new Point(0, 500), new Point(800, 100), 500);
		player.setImage(10);
		
		bg = new Background(13, new Point(0, 0), new Point(1600, 1000));
		bg.setImage(89);
		
		timeLevel = new TimeLevel(19, 300, new Point(360, 0), new Point(80, 40));
		
		managerAlerts = new RandomManager(levelDificulty);
		
		tecnico = new Tecnico(new Button(new Point(100,500), new Point(100, 100), 14, 27, messageTecnico));
		antena = new ConsertaAntena(new Button(new Point(200, 500), new Point(100, 100),15, 28, messageAntena));
		poste = new ConsertaPoste(new Button(new Point(300,500), new Point(100, 100), 16, 29, messagePoste));
		
		Loja = new Button(new Point(100,100), new Point(100,100), 21,21, new GameObject(new Point(100,100), new Point(200,180)));
		
		loja = new LojaManager();
		sairLoja = new GameObject(new Point(300,100), new Point(80,50));
		sairLoja.setImage(22);
		
		/*camUp = new GameObject(new Point(350, 0), new Point(100, 20));
		camUp.setImage(40);
		camRight = new GameObject(new Point(780, 200), new Point(20, 100));
		camRight.setImage(41);
		camLeft = new GameObject(new Point(0, 200), new Point(20, 100));
		camLeft.setImage(42);
		camDown = new GameObject(new Point(350, 480), new Point(100, 20));
		camDown.setImage(43);*/
	}
	protected void Initialze() 
	{
		
	}
	public void update() 
	{
		objetosGame.Update();
		player.Update();
		timeLevel.Update();
		managerAlerts.Update();
		Contador += Time.getInstance().loopTime;
		
		if(!LojaAtiva)
		this.Loja.Update();
		
		numeroTecnico = Personagem.Tecnicos;
		numeroPoste = Personagem.ConsertaPoste;
		numeroAntena = Personagem.ConsertaAntena;
		
		if(MouseReleased)
		{
			if(tecnico.btnRepresentativo.getRectangle().contains(Mouse.getInstance().getPositionMouse()) 
					  && Mouse.getInstance().isMousePressed(MouseEvent.BUTTON1))
			{
				trabalhadorSelecionado = tecnico;
				trabalhadorSelecionado.ID = 1;
				MouseReleased = false;
				System.out.println("Selecionei trabalhador 1");
			}
			else if(antena.btnRepresentativo.getRectangle().contains(Mouse.getInstance().getPositionMouse()) 
					  && Mouse.getInstance().isMousePressed(MouseEvent.BUTTON1))
			{
				trabalhadorSelecionado = antena;
				trabalhadorSelecionado.ID = 3;
				MouseReleased = false;
				System.out.println("Selecionei trabalhador 3");
			}
			else if(poste.btnRepresentativo.getRectangle().contains(Mouse.getInstance().getPositionMouse()) 
					  && Mouse.getInstance().isMousePressed(MouseEvent.BUTTON1))
			{
				trabalhadorSelecionado = poste;
				trabalhadorSelecionado.ID =2;
				MouseReleased = false;
				System.out.println("Selecionei trabalhador 2");
			}
		}
		if(!Mouse.getInstance().isMousePressed(MouseEvent.BUTTON1))
			MouseReleased = true;
		
		if(this.Loja.getRectangle().contains(Mouse.getInstance().getPositionMouse()) &&
				Mouse.getInstance().isMousePressed(MouseEvent.BUTTON1))
		{
			this.LojaAtiva = true;
		}
		if(!this.LojaAtiva)
		{
			if(this.Contador > 6)
			{
				this.valueID = this.managerAlerts.sortear(); // consertar
				
				this.Contador = 0;
			}
			if(valueID >= 1)
			{
				valueObject = this.objetosGame.sortear(valueID);
				this.objetosGame.getList(valueID).get(valueObject).clientStatus.activated = true;
				valueID = 0;
				System.out.println("ativei");
			}
		}
		else
		{
			loja.Update();
			if(sairLoja.getRectangle().contains(Mouse.getInstance().getPositionMouse()) && Mouse.getInstance().isMousePressed(MouseEvent.BUTTON1))
			{
				LojaAtiva = false;
			}
		}
		
		LinkedList<ObjetosCenario> objs = objetosGame.allObjects();
		for(int i =0; i< objs.size(); i++)
		{
			if(objs.get(i).clientStatus.activated == true)
			{
				if(trabalhadorSelecionado != null)
				{
					if(objs.get(i).foiClicado == true && objs.get(i).clientStatus.activated)
					{
						boolean NoJob = true;
						switch(trabalhadorSelecionado.ID)
						{
						case 1:
							if(Personagem.Tecnicos > 0)
							{
								NoJob = false;
								Personagem.Tecnicos -=1;
							}
							break;
						case 2:
							if(Personagem.ConsertaPoste > 0)
							{
								NoJob = false;
								Personagem.ConsertaPoste -=1;
							}
							break;
						case 3:
							if(Personagem.ConsertaAntena > 0)
							{
								NoJob = false;
								Personagem.ConsertaPoste -=1;
							}
							break;
						}
						if(NoJob == false)
						{
							Personagem.Money += objs.get(i).clientStatus.GetMoney();
							objs.get(i).setHudSettings(trabalhadorSelecionado, trabalhadorSelecionado.ID);
							objs.get(i).clientStatus.activated = false;
						}
					}
				}
			}
		}
		tecnico.Update();
		antena.Update();
		poste.Update();
		CameraInput();
	}
	
	public void CameraInput()
	{
		if (Mouse.getInstance().getPositionMouse().x < 100 && Mouse.getInstance().getPositionMouse().x > 0)
			Camera.getInstance().MoveCamera(-speed, 0);
		if (Mouse.getInstance().getPositionMouse().x > 700 && Mouse.getInstance().getPositionMouse().x < 800)
			Camera.getInstance().MoveCamera(speed, 0);
		if (Mouse.getInstance().getPositionMouse().y < 100 && Mouse.getInstance().getPositionMouse().y > 0 &&
			Mouse.getInstance().getPositionMouse().x > 0 && Mouse.getInstance().getPositionMouse().x < 800)
			Camera.getInstance().MoveCamera(0, -speed);
		if (Mouse.getInstance().getPositionMouse().y > 400 && Mouse.getInstance().getPositionMouse().y < 500 &&
			Mouse.getInstance().getPositionMouse().x > 0 &&	Mouse.getInstance().getPositionMouse().x < 800)
			Camera.getInstance().MoveCamera(0, speed);
	}
	
	public void draw(Graphics2D g2d) 
	{
		Point2D camPos = Camera.getInstance().worldPosition;
		
		bg.draw(g2d, Camera.getInstance());
		
		objetosGame.draw(g2d, Camera.getInstance());
		
		player.Draw(g2d);
		timeLevel.Draw(g2d);
		
		if(!LojaAtiva)
		this.Loja.draw(g2d);
		
		tecnico.Draw(g2d);
		antena.Draw(g2d);
		poste.Draw(g2d);
	
		if(LojaAtiva)
		{
			loja.Draw(g2d);
			sairLoja.draw(g2d);
		}
		
		
		g2d.drawString("" + SlotTecnico, 159,587);
		g2d.drawString("" + SlotConsertaPoste, 261,587);
		g2d.drawString("" + SlotConsertaAntena, 365,587);
		g2d.drawString("" + numeroTecnico, 124,587);
		g2d.drawString("" + numeroPoste, 226,587);
		g2d.drawString("" + numeroAntena, 330,587);
		
		/*if (camPos.getY() > 0)
			camUp.draw(g2d);
		if (camPos.getX() > 0)
			camLeft.draw(g2d);
		if (camPos.getX() + Camera.getInstance().viewportWidth < Camera.getInstance().worldWidth)
			camRight.draw(g2d);
		if (camPos.getY() + Camera.getInstance().viewportHeight < Camera.getInstance().worldHeight)
			camDown.draw(g2d);*/
	}
}


